#include "..\inc\FileHash.h"
//#include <fstream>
#include <md5Poco.h>
//#include <iostream>

using namespace std;
typedef __int64 int64_t;
FileHash::FileHash(void)
{
}

FileHash::~FileHash(void)
{
}

bool FileHash::GenFileXml(const std::string& aFilePath, const std::string& aFileResult)
{	
	fstream ifs;//("C:\\test1.txt", ios::in | ios::binary);
	ifs.open(aFilePath.c_str(), ios::in | ios::binary);

	ifs.seekg(0, ios::end);
	int64_t asize = ifs.tellg();
	ifs.seekg(0, ios::beg);


	fstream fXML(aFileResult.c_str(), ios::out | ios::binary | ios::trunc);
	fXML<<"<Files>"<<endl;
	fXML<<"		<Name>"<<aFilePath<<"</Name>"<<endl;
	fXML<<"		<Size>"<<asize<<"</Size>"<<endl;
	fXML<<"		<Pieces>"<<endl;

	int64_t aSizePiece = 1000000;
	int64_t aTotalSeeg = 0;
	if(aSizePiece >= asize)
	{
		fXML<<"		<Manh>"<<endl;
		fXML<<"			<Size>"<<asize<<"</Size>"<<endl;
		fXML<<"			<MD5>"<<CMD5::GetMD5ofFile(aFilePath)<<"</MD5>"<<endl;
		fXML<<"		</Manh>"<<endl;
		//fXML<<"</Files>";
	}
	else
	{
		

		while (aTotalSeeg + aSizePiece <= asize )
		{
			char* aStringPiece = new char[aSizePiece+1];
			ifs.read(aStringPiece, aSizePiece);
                        aStringPiece[aSizePiece] = 0;
			fXML<<"		<Manh>"<<endl;
			fXML<<"			<Size>"<<aSizePiece<<"</Size>"<<endl;
			fXML<<"			<MD5>"<<CMD5::GetMD5(aStringPiece , aSizePiece)<<"</MD5>"<<endl;
//			cout<<aStringPiece<<endl;
			fXML<<"		</Manh>"<<endl;
			aTotalSeeg += aSizePiece;
			delete[] aStringPiece;
			//cout<<aStringPiece<<endl;
		}
		if(aTotalSeeg <asize)
		{
			//char aStringPiece[1024];
			char* aStringPiece = new char[asize - aTotalSeeg+1];
			ifs.read(aStringPiece, asize - aTotalSeeg);
                        aStringPiece [asize - aTotalSeeg] = 0;
			fXML<<"		<Manh>"<<endl;
			fXML<<"			<Size>"<<asize - aTotalSeeg<<"</Size>"<<endl;
			fXML<<"			<MD5>"<<CMD5::GetMD5(aStringPiece , asize - aTotalSeeg)<<"</MD5>"<<endl;
			fXML<<"		</Manh>"<<endl;
			
			aTotalSeeg += aSizePiece;
			delete [] aStringPiece;
		}
		

	}
	fXML<<"		</Pieces>"<<endl;
	fXML<<"</Files>";

	ifs.close();
	fXML.close();



	return true;
}

bool FileHash::GenFileXml(const std::string& aFilePath, const std::string& aFileResult, int aPieceSize)
{
        fstream ifs;//("C:\\test1.txt", ios::in | ios::binary);
        ifs.open(aFilePath.c_str(), ios::in | ios::binary);

        ifs.seekg(0, ios::end);
        int64_t asize = ifs.tellg();
        ifs.seekg(0, ios::beg);


        fstream fXML(aFileResult.c_str(), ios::out | ios::binary | ios::trunc);
        fXML<<"<Files>"<<endl;
        fXML<<"		<Name>"<<aFilePath<<"</Name>"<<endl;
        fXML<<"		<Size>"<<asize<<"</Size>"<<endl;
        fXML<<"		<Pieces>"<<endl;

        int64_t aSizePiece = aPieceSize;
        int64_t aTotalSeeg = 0;
        if(aSizePiece >= asize)
        {
                fXML<<"		<Manh>"<<endl;
                fXML<<"			<Size>"<<asize<<"</Size>"<<endl;
                fXML<<"			<MD5>"<<CMD5::GetMD5ofFile(aFilePath)<<"</MD5>"<<endl;
                fXML<<"		</Manh>"<<endl;
                //fXML<<"</Files>";
        }
        else
        {


                while (aTotalSeeg + aSizePiece <= asize )
                {
                        char* aStringPiece = new char[aSizePiece+1];
                        ifs.read(aStringPiece, aSizePiece);
                        aStringPiece[aSizePiece] = 0;
                        fXML<<"		<Manh>"<<endl;
                        fXML<<"			<Size>"<<aSizePiece<<"</Size>"<<endl;
                        fXML<<"			<MD5>"<<CMD5::GetMD5(aStringPiece , aSizePiece)<<"</MD5>"<<endl;
//			cout<<aStringPiece<<endl;
                        fXML<<"		</Manh>"<<endl;
                        aTotalSeeg += aSizePiece;
                        delete[] aStringPiece;
                        //cout<<aStringPiece<<endl;
                }
                if(aTotalSeeg <asize)
                {
                        //char aStringPiece[1024];
                        char* aStringPiece = new char[asize - aTotalSeeg+1];
                        ifs.read(aStringPiece, asize - aTotalSeeg);
                        aStringPiece [asize - aTotalSeeg] = 0;
                        fXML<<"		<Manh>"<<endl;
                        fXML<<"			<Size>"<<asize - aTotalSeeg<<"</Size>"<<endl;
                        fXML<<"			<MD5>"<<CMD5::GetMD5(aStringPiece , asize - aTotalSeeg)<<"</MD5>"<<endl;
                        fXML<<"		</Manh>"<<endl;

                        aTotalSeeg += aSizePiece;
                        delete [] aStringPiece;
                }


        }
        fXML<<"		</Pieces>"<<endl;
        fXML<<"</Files>";

        ifs.close();
        fXML.close();



        return true;
}


bool FileHash::GenFileXml(const std::string& aFilePath, fstream& fXML)
{	
	fstream ifs;//("C:\\test1.txt", ios::in | ios::binary);
	ifs.open(aFilePath.c_str(), ios::in | ios::binary);

	ifs.seekg(0, ios::end);
	int64_t asize = ifs.tellg();
	ifs.seekg(0, ios::beg);


	//fstream fXML(aFileResult.c_str(), ios::out | ios::binary | ios::trunc);
	fXML<<"<Files>"<<endl;
	fXML<<"		<Name>"<<aFilePath<<"</Name>"<<endl;
	fXML<<"		<Size>"<<asize<<"</Size>"<<endl;
	fXML<<"		<Pieces>"<<endl;

	int64_t aSizePiece = 1000;
	int64_t aTotalSeeg = 0;
	if(aSizePiece >= asize)
	{
		fXML<<"		<Manh>"<<endl;
		fXML<<"			<Size>"<<asize<<"</Size>"<<endl;
		fXML<<"			<MD5>"<<CMD5::GetMD5ofFile(aFilePath)<<"</MD5>"<<endl;
		fXML<<"		</Manh>"<<endl;
		//fXML<<"</Files>";
	}
	else
	{

		while (aTotalSeeg + aSizePiece <= asize )
		{
			char aStringPiece[1024];
			ifs.read(aStringPiece, aSizePiece);
			fXML<<"		<Manh>"<<endl;
			fXML<<"			<Size>"<<aSizePiece<<"</Size>"<<endl;
			fXML<<"			<MD5>"<<CMD5::GetMD5(aStringPiece)<<"</MD5>"<<endl;
			fXML<<"		</Manh>"<<endl;
			aTotalSeeg += aSizePiece;
			//cout<<aStringPiece<<endl;
		}
		if(aTotalSeeg <asize)
		{
			char aStringPiece[1024];
			ifs.read(aStringPiece, asize - aTotalSeeg);
			fXML<<"		<Manh>"<<endl;
			fXML<<"			<Size>"<<asize - aTotalSeeg<<"</Size>"<<endl;
			fXML<<"			<MD5>"<<CMD5::GetMD5(aStringPiece)<<"</MD5>"<<endl;
			fXML<<"		</Manh>"<<endl;

			aTotalSeeg += aSizePiece;
		}

	}
	fXML<<"		</Pieces>"<<endl;
	fXML<<"</Files>";
	
	ifs.close();
	//fXML.close();

	return true;
}
